<?php

class Admin_Table_Motohelp_Events extends Apk_Db_Table
{
    protected $_name      = 'road_events';
    protected $_primary   = 'id';
    protected $_ERROR = '';

    public function GetError()
    {
    	return $this->_ERROR;
    }

    public function GetDataList($page=1, $sort=null, $page_count=null, $params=null)
    {
    	//$time_arr = getdate();
    	//$public_date = mktime(0,0,0, $time_arr['mon']-2, $time_arr['mday'], $time_arr['year']);

    	$sel = $this->getAdapter()->select()
                    ->from($this->_name)
    		  //->from($this->_name, array('id', 'title', 'categories.menu_name_ru', 'public_date', 'is_active', 'is_top'))
    		  //->joinLeft('categories', 'categories.id = events.id_cat', array())
    		  ->where($this->_name.'.is_ghost = 0')
    		  ->where($this->_name.'.is_removed = 0');

		if(count($params) > 0)
		foreach($params as $k => $v)
		{
			switch ($k)
			{
//				case 'popular':
//					$sel->order(array('articles.rating desc', 'articles.rating_count desc', 'articles.public_date desc'));
//					$sel->where('public_date > ?', $public_date)
//						->where('type = 0')
//						->where('is_top = 0');
//					break;
//				case 'cats':
//					if(count($v) > 0)
//					$sel->where('id_cat in (?)', $v);
//					break;
//				case 'is_top':
//				case 'is_active':
//					if(intval($v) >= 0 && trim($v) != '')
//					$sel->where($this->_name.'.'.$k.' = ?', intval($v));
//					break;
//				case 'type':
//					if(intval($v) >= 0)
//					$sel->where($this->_name.'.'.$k.' = ?', intval($v));
//					break;
//				case 's':
//					if(trim($v) != '')
//					{
//						$sel->where($this->_name.'.title like \'%'.$v.'%\' or '.$this->_name.'.desc like \'%'.$v.'%\'  ');
//						//$sel->group($this->_name.'.id');
//					}
//					break;
				default:
					if(intval($v) > 0)
					$sel->where($this->_name.'.'.$k.' = ?', $v);
			}
		}


		if(!is_null($sort) && strlen($sort) > 0) $sel->order($this->_name.'.'.$sort);
		else $sel->order($this->_name.'.event_date desc');

//				echo $sel->assemble();die();

    	return $this->setDecorator('simpleDecorator')->getPager($sel, $page, array('perPage' => $page_count));
    }

    public function GetDataRow($id)
    {
    	$sel = $this->getAdapter()->select()
    				->from($this->_name)
    				->where('id = ?', $id);

    	return $this->setDecorator('simpleDecorator')->getList($sel)->getRow(0)->toArray();
    }


    public function GetCommentsList($id, $page=1, $sort=null, $page_count=null, $params=null)
    {
    	$sel = $this->getAdapter()->select()
    		  ->from('articles_comments', array('id', 'text', 'add_date'))
    		  ->where('id_article = ?', $id);


		if(count($params) > 0)
		foreach($params as $k => $v)
		{
			switch ($k)
			{
				default:
					if(intval($v) > 0)
					$sel->where('articles_comments.'.$k.' = ?', $v);
			}
		}

		if(!is_null($sort) && strlen($sort) > 0) $sel->order('articles_comments.'.$sort);
		else $sel->order('articles_comments.add_date desc');

    	return $this->setDecorator('simpleDecorator')->getPager($sel, $page, array('perPage' => $page_count));
    }

    public function GetCommentsRow($id)
    {
    	$sel = $this->getAdapter()->select()
    		  ->from('articles_comments')
    		  ->where('id = ?', $id);

    	return $this->setDecorator('simpleDecorator')->getList($sel)->getRow(0)->toArray();
    }

    public function GetLangList($a=null)
    {
    	$sel = $this->getAdapter()->select()
    			->from('languages')
    			->where('is_active')
    			->order('id');
    	$data = $this->getList($sel)->toArray();

    	if($a > 0)
    	foreach ($data as $k => $v)
    		if($v['id'] == $a) $data[$k]['__attributes']['active'] = 1;

    	return $data;
    }

    //*******************
    // insert/update/delete

	public function insert(array $data, $user_id)
    {
    	$data['modify_date'] = new Zend_Db_Expr("now()");
    	$data['add_date'] = new Zend_Db_Expr("now()");
    	$data['add_user_id'] = $user_id;
    	$data['modify_user_id'] = $user_id;

        parent::insert($data);
        return $this->getAdapter()->lastInsertId($this->_name);
        
    }

    public function update(array $data, $id, $user_id)
    {
    	$data['modify_date'] = new Zend_Db_Expr("now()");
        $data['event_date']   = date("Y-m-d H:i:s", strtotime(@$data['event_date'] . ':00'));
    	$data['modify_user_id'] = $user_id;
    	$data['is_ghost'] = 0;

		return parent::update($data, 'id = '.$id);
    }

    public function update_all(array $data, $id, $user_id)
    {
    	$data['modify_date'] = time();
    	$data['modify_user_id'] = $user_id;
    	$data['is_ghost'] = 0;

		return parent::update($data, 'id = '.$id);
    }

    public function remove($items, $user_id)
    {
    	foreach($items as $k => $id)
    	{
	    	$data['modify_user_id'] = $user_id;
	    	$data['modify_date'] = time();
	    	$data['is_removed'] = 1;

	    	parent::update($data, 'id = '.$id);
    	}

    	return true;
	}

    public function remove_commetns($items, $aid)
    {
    	foreach($items as $k => $id)
    	{
	    	$this->getAdapter()->delete('articles_comments', 'id = '.$id);
    	}

    	$this->commetns_count($aid);

    	return true;
	}

    public function commetns_count($id)
    {
    	$sel = $this->getAdapter()->select()
    					->from('articles_comments', array('count' => 'count(id)'))
    					->where('id_article = ?', $id);
    	$count = $this->getList($sel)->getRow(0)->toArray();

    	$d['comments_count'] = $count['count'];
    	$this->update($d, 'id = '.$id);
    }

	public function comment_insert(array $data, $user_id = 0)
	{
		$data['modify_date'] = time();
    	$data['add_date'] = time();
    	$data['add_user_id'] = $user_id;
    	$data['modify_user_id'] = $user_id;

        $this->getAdapter()->insert('articles_comments', $data);
        return $this->getAdapter()->lastInsertId('articles_comments');
    }

	public function comment_update(array $data, $id, $user_id = 0)
	{
    	$data['modify_date'] = time();
    	$data['modify_user_id'] = $user_id;
    	$data['is_ghost'] = 0;

        return $this->getAdapter()->update('articles_comments', $data, 'id = '.$id);
    }

    public function photo_count($id)
    {
    	$sel = $this->getAdapter()->select()
    					->from('objects', array('count' => 'count(id)'))
    					->where('is_active')
    					->where('is_ghost = 0')
    					->where('is_removed = 0')
    					->where('id_article = ?', $id);

    	$count = $this->getList($sel)->getRow(0)->toArray();

    	$d['count_photo'] = $count['count'];
    	parent::update($d, 'id = '.$id);
    }
    
    
    //************************************************/
    // Getting data for listing
    //************************************************/

    public function GetEventTypeList($a = null, $role = 1)
    {
    	$ret = array();

		$sel = $this->getAdapter()->select()
						->from('dir_type_event', array('id', 'name'))
						->order('id');

		//if($role != 1) $sel->where('id != 1');

		$data = $this->getList($sel)->toArray();

    	if(count($data) > 0 && !is_null($a))
    	foreach($data as $k => $v)
    	  	if($v['id'] == $a) $data[$k]['__attributes']['active'] = 1;

    	return $data;
    }
    
    
    public function GetDistrictKievList($a = null, $role = 1)
    {
    	$ret = array();

		$sel = $this->getAdapter()->select()
						->from('dir_district_kiev', array('id', 'name'))
						->order('id');

		//if($role != 1) $sel->where('id != 1');

		$data = $this->getList($sel)->toArray();

    	if(count($data) > 0 && !is_null($a))
    	foreach($data as $k => $v)
    	  	if($v['id'] == $a) $data[$k]['__attributes']['active'] = 1;

    	return $data;
    }

    public function GetReactionTypeList($a = null, $role = 1)
    {
    	$ret = array();

		$sel = $this->getAdapter()->select()
						->from('dir_reaction_type', array('id', 'name'))
						->order('id');

		//if($role != 1) $sel->where('id != 1');

		$data = $this->getList($sel)->toArray();

    	if(count($data) > 0 && !is_null($a))
    	foreach($data as $k => $v)
    	  	if($v['id'] == $a) $data[$k]['__attributes']['active'] = 1;

    	return $data;
    }

    /********************
     ***  Decorators  ***
    *********************/

    public function simpleDecorator($key, $value)
    {
        switch ($key) {
            case 'modify_date':
            case 'add_date':
            case 'event_date':
            	if ($value == "" || $value === null) {
                    $result[$key] = '';
                } else {
                    $date = new DateTime($value);
                    $result[$key] = $date->format('d.m.Y H:i');
                }
            	break;
            default:
                $result[$key] = stripslashes($value);
                break;
        }
        return $result;
    }



}
?>